/**
 * 
 */
package d.qapaper.tool;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Kaven
 * 2011-5-24
 */
public class ClassTypeUtil {
	  final static public int INTEGER = 0;
	    final static public int FLOAT = 1;
	    final static public int DOUBLE = 2;
	    final static public int BIGDECIMAL = 3;
	    final static public int BOOLEAN = 4;
	    final static public int STRING = 5;
	    final static public int DATE_SQL = 6;
	    final static public int TIMESTAMP = 7;
	    final static public int SYBASETIME = 8;
	    final static public int LONG = 9;
	    final static public int DATE_UTIL = 10;
	    final static public int Byte = 11;
	    final static public int COLLECTION = 12;
	    final static public int LIST = 13;
	    final static public int MAP = 14;
	    final static public int SET = 15;
	    final static private HashMap map = new HashMap();

	    static {
	        map.put("java.lang.Integer", String.valueOf(INTEGER));
	        map.put("java.lang.Float", String.valueOf(FLOAT));
	        map.put("java.lang.Double", String.valueOf(DOUBLE));
	        map.put("java.math.BigDecimal", String.valueOf(BIGDECIMAL));
	        map.put("java.lang.Boolean", String.valueOf(BOOLEAN));
	        map.put("java.lang.String", String.valueOf(STRING));
	        map.put("java.sql.Date", String.valueOf(DATE_SQL));
	        map.put("java.util.Date", String.valueOf(DATE_UTIL));
	        map.put("java.sql.Timestamp", String.valueOf(TIMESTAMP));
	        map.put("com.sybase.jdbc2.tds.SybTimestamp", String.valueOf(SYBASETIME));
	        map.put("java.lang.Long", String.valueOf(LONG));
	        map.put("java.lang.Byte", String.valueOf(Byte));
	        map.put("java.util.Collection", String.valueOf(COLLECTION));
	        map.put("java.util.List", String.valueOf(LIST));
	        map.put("java.util.Map", String.valueOf(MAP));
	        map.put("java.util.Set", String.valueOf(SET));
	    }

	    /**
	     * 得到类的编号，如果编号为-1则表明aido没有为此种类型编号
	     *
	     * @param _cls
	     * @return
	     */
	    public static int getTypeByClass(Class _cls) {
	        return getTypeByClassName(_cls.getName());
	    }

	    public static int getTypeByClass(String _clsName) {
	        return getTypeByClassName(_clsName);
	    }

	    /**
	     * 得到类的编号，如果编号为-1则表明dcore没有为此种类型编号
	     *
	     * @param _clsName
	     * @return
	     */
	    public static int getTypeByClassName(String _clsName) {
	        String riv = (String) map.get(_clsName);
	        if (riv == null) return -1;
	        return Integer.valueOf(riv);
	    }

	    /**
	     * 判断是否是数字类型
	     *
	     * @param _cls
	     * @return
	     */
	    public static boolean isNumberType(Class _cls) {
	        return isNumberType(_cls.getName());
	    }

	    public static boolean isNumberType(String _clsName) {
	        switch (getTypeByClass(_clsName)) {
	            case INTEGER:
	                return true;
	            case FLOAT:
	                return true;
	            case DOUBLE:
	                return true;
	            case BIGDECIMAL:
	                return true;
	            case LONG:
	                return true;
	            default:
	                return false;
	        }
	    }

	    /**
		 * 验证参数类型
		 * 包括接口和继承类
		 * @param cls 类 方法参考类
		 * @param objCls 参考类
		 * @return 返回true表示参数类型相同,false表示参数类型不相同
		 */
		@SuppressWarnings("unchecked")
		private static boolean checkType(Class cls, Class objCls) {
			if (cls.getName().equals(objCls.getName()))
				return true;
			Class[] ifCls = objCls.getInterfaces();
			if (ifCls != null && ifCls.length > 0) {
				for (Class icls : ifCls) {
					if (icls.getName().equals(cls.getName()))
						return true;
					if (icls.getSuperclass() != null) {
						objCls = objCls.getSuperclass();
						boolean tempBl = checkType(cls, objCls);
						if (tempBl)
							return true;
					}
				}
			}
			if (objCls.getSuperclass() != null) {
				objCls = objCls.getSuperclass();
				return checkType(cls, objCls);
			}
			return false;
		}

	    /**
	     * 判断类型是否是LIST
	     * @param cls
	     * @return
	     */
	    public static boolean isList(Class cls){
	        return checkType(List.class,cls);
	    }

	    /**
	     * 判断类型是否是Collection
	     * @param cls
	     * @return
	     */
	    public static boolean isCollection(Class cls){
	        return checkType(Collection.class,cls);
	    }

	    /**
	     * 判断类型是否是Map
	     * @param cls
	     * @return
	     */
	    public static boolean isMap(Class cls){
	        return checkType(Map.class,cls);
	    }
	    /**
	     * 判断类型是否是Set
	     * @param cls
	     * @return
	     */
	    public static boolean isSet(Class cls){
	        return checkType(Set.class,cls);
	    }
}
